﻿$(function () {
    //配件等级
    $("#AccessoriesTypeID").change(function () {
        var typeId = $("#AccessoriesTypeID").val();
        if (typeId > 0)
            $.ajax({
                url: "/Common/GetAccessoriesLevelList?AccessoriesTypeID=" + typeId,
                success: function (data) {
                    var html = "<option value=''>请选择</option>";
                    if (data && data.length > 0) {
                        for (var i = 0; i < data.length; i++) {
                            html += "<option value='";
                            html += data[i].Value;
                            html += "'>" + data[i].Text;
                            html += "</option>";
                        }
                    }
                    $("#AccessoriesLevelID").html(html);
                }
            });
    });
    //选择省份
    $(".Province").change(function () {
        provinceChange(this);
    });
    $(".Province").change();
    //选择图片
    $(".file").on("change", "input[type='file']", function () {
        var filePath = "";
        //多文件上传简单处理，对于支持HTML5的可以多文件上传，不支持H5的只能单个上传
        if (window.File && window.FileList) {
            var fileList = this.files;
            for (var i = 0; i < fileList.length; i++) {
                filePath += fileList[i].name + "&nbsp;&nbsp;&nbsp;";
            }
        } else {
            filePath = $(this).val();
        }
        if (filePath.indexOf("jpg") != -1 || filePath.indexOf("png") != -1) {
            var arr = filePath.split('\\');
            var fileName = arr[arr.length - 1];
            $(this).parent().prev().html(fileName);
        } else {
            $(this).parent().prev().html("");
            layer.alert("请选择png或jpg文件");
            return;
        }
    });
    //上传通融详情
    $("#uploadBtn").click(function () {
        var insuranceCompanyBendTheRulesLossAdjusterName = $("#InsuranceCompanyBendTheRulesLossAdjusterName").val();
        var remark = $("#Remark").val();
        if (insuranceCompanyBendTheRulesLossAdjusterName == "") {
            layer.alert("请填写通融理赔员！");
            return false;
        }
        if (remark == "") {
            layer.alert("通融详情不能为空！");
            return false;
        }
        var uploadFileName = $(".showFileName").text();
        if (!uploadFileName || uploadFileName == "") {
            layer.alert("请选择要上传的图片！");
            return false;
        }
        var form = document.getElementById('bendTheRulesForm');
        var formData = new FormData(form);

        layer.load();//弹出遮罩层

        $.ajax({
            url: "/WorkOrderCommonInfo/SaveInsuranceWarrantyBendTheRulesRecord?InsuranceWarrantyRecordID=" + $("#InsuranceWarrantyRecordID").val(),
            type: "post",
            data: formData,
            processData: false,
            contentType: false,
            success: function (data) {
                layer.alert("保存成功！", {
                        closeBtn: 0,
                        yes: function () {
                            $("#divBendTheRulesRecordInfo").html(data);
                            layer.closeAll();
                            form.reset();
                        }
                    }
                );
            }
        });
        return true;
    });
});


/**
  * 搜索维修服务商
  */
function searchProviderBranch() {
    queryParamList.ProvinceId = $("#ProviderBranchProvinceID").val();
    queryParamList.CityId = $("#ProviderBranchCityID").val();
    queryParamList.AccessoriesTypeID = $("#AccessoriesTypeID").val();
    queryParamList.AccessoriesLevelID = $("#AccessoriesLevelID").val();
    queryParamList.WarrantyMaintenanceType = $("#WarrantyMaintenanceTypeID").val();
    $("#tabServiceProviderBranch").dataTable().fnDraw();
}
/**
   *选择维修服务商
   * @@param {} obj
   * @@returns {}
   */
function checkServiceBranch(obj) {
    if ($(obj).prop("checked")) {
        $("#ServiceProviderBranchId").val(obj.value);
    } else {
        $("#ServiceProviderBranchId").val("");
    }
}
/**
 * 省份切换带出城市
 * @param {} obj 
 * @returns {} 
 */
function provinceChange(obj) {
    var provinceId = $(obj).val();
    if (provinceId > 0)
        $.ajax({
            url: "/Common/GetCityList?provinceID=" + provinceId,
            success: function (data) {
                var html = "<option value='0'>请选择</option>";
                if (data && data.length > 0) {
                    for (var i = 0; i < data.length; i++) {
                        html += "<option value='";
                        html += data[i].Value;
                        html += "'>" + data[i].Text;
                        html += "</option>";
                    }
                }
                $("#" + $(obj).attr("id").replace("ProvinceID", "CityID")).html(html);
            }
        });
}
/**
 * 搜索分拣中心
 * @returns {} 
 */
function searchSortingCenter() {
    queryParamList.ProvinceId = $("#SortingCenterBelongProvinceID").val();
    queryParamList.CityId = $("#SortingCenterBelongCityID").val();
    $("#tabSortingCenter").dataTable().fnDraw();
}
function resetSortingCenter() {
    $("#SortingCenterBelongProvinceID").val("");
    $("#SortingCenterBelongCityID").html("");
    searchSortingCenter();
}
/**
    *选择分拣中心
    * @@param {} obj
    * @@returns {}
    */
function checkSortingCenter(obj) {
    if ($(obj).prop("checked")) {
        $("#SortingCenterID").val(obj.value);
        if ($("#WarrantyMaintenanceTypeID").val() == 1) {
            var id = $(obj).parent().siblings('td');
            $('#insuranceWarrantyRecordExpressInfo_ReceiverUserName').val(id.eq(4).text());
            $('#insuranceWarrantyRecordExpressInfo_ReceiverUserPhone').val(id.eq(5).text());
            $('#insuranceWarrantyRecordExpressInfo_ReceiverUserAddress').val(id.eq(3).text());
        }
    } else {
        $("#SortingCenterID").val("");
    }
}

//删除图片
function deleteImage(obj,id) {
    layer.confirm('确定要删除吗？', {
        yes: function (result) {
            if (result) {
                $.ajax({
                    url: "/WorkOrderCommonInfo/DeleteInsuranceWarrantyBendTheRulesRecord?ID=" + id,
                    type: "post",
                    processData: false,
                    contentType: false,
                    success: function (data) {
                        if (data.success) {
                            layer.alert(data.message, {
                                 yes: function () {
                                    $(obj).parent().parent().remove();
                                     layer.closeAll();
                                 }
                            });
                        } else {
                            layer.alert(data.message);
                        }
                    }
                });
            }
        }, icon: 3
    });
   
}
